﻿using GKQC.Common;
using GKQC.DbHelper.Dao;
using GKQC.DbHelper.DbModels;
using GKQC.DbHelper.Services;
using GKQC.DbHelper.ViewModels;
using GKQC.Forms;
using GKQC.Language;
using GKQC.Machine;
using GKQC.Ort;
using GKQC.Report;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Reflection;
using System.Text;
using System.Windows.Forms;

namespace GKQC
{
    public partial class MainForm : Form
    {
        Dictionary<string, Form> _DicForm = new Dictionary<string, Form>();
        UserModel user = null;
        AllDbContext dbContext = null;
        //readonly string noRightString = "權限不足！";
        //readonly string msgTitle = "提示";
        List<BaseDataModel> BillTypes = new List<BaseDataModel>();

        public MainForm(DbHelper.DbModels.UserModel user)
        {
            InitializeComponent();
            
            try
            {
                dbContext = new AllDbContext();
                InitialLeftMenu();
                this.user = user;
                CommonString commonString = Settings.GetDataObjectFile<CommonString>("CommonString.Dat");
                if (commonString != null)
                    tssDataBase.Text = "檢測系統：" + commonString.S1 + " " + commonString.S2;// + "    ERP：" + commonString.S11;

                if (user != null)
                    tssUser.Text = "用戶：" + user.UserName;

                if (user.UserCode != "oubl") tsmQuery.Visible = false;
                else tsmQuery.Visible = true;

                Version curVer = Assembly.GetExecutingAssembly().GetName().Version; //获取当前程序版本
                this.toolStripStatusLabel3.Text = "版本: " + curVer.ToString();
                QcFormBase.Version = curVer.ToString();

                GetConver();

                LanguageHelper.SetControlLanguageText(this);

                UpdateScriptHelper.UpdateScript(dbContext);

                var level = SysParamService.GetSysParamValue(dbContext, "PriorityLevel");
                if (level == "A")
                    OrtFilter.NoBad = true;

                DownloadHelpExe();
            }
            catch (Exception ex)
            {
                Logger.LogError(ex);
                QMessageBox.Show(ex.Message, "異常S0009", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        void DownloadHelpExe()
        {

            BackgroundWorker w = new BackgroundWorker();
            w.DoWork += (s, a) =>
            {
                try
                {
                    string zipfile = "updatefile.zip";
                    string versionfile = "updateversion.txt";
                    string zipfile2 = Path.Combine(Settings.GetLocalUpdatePath(), zipfile);
                    string versionfile2 = Path.Combine(Settings.GetLocalUpdatePath(), versionfile);
                    Logger.LogMessage("zipfile2:" + zipfile2);
                    Logger.LogMessage("versionfile2:" + versionfile2);

                    ApiHelper.DownloadFile("http://www.osoner.cn/upload/files/elite/updateversion.txt", versionfile2);
                    if (File.Exists(versionfile2))
                    {
                        var updatePath = SysParamService.GetSysParamValue(new AllDbContext(), "UpdatePath");
                        if(string.IsNullOrWhiteSpace(updatePath))
                            updatePath = AppDomain.CurrentDomain.BaseDirectory + "update";

                        var localVersionFile = Path.Combine(updatePath, versionfile);
                        var localUpdateFile = Path.Combine(updatePath, zipfile);
                        Logger.LogMessage("localVersionFile:" + localVersionFile);
                        Logger.LogMessage("localUpdateFile:" + localUpdateFile);
                        var updateVersionStringOld = Tools.ReadTextFileFirstRow(localVersionFile);

                        var updateVersionStringNew = Tools.ReadTextFileFirstRow(versionfile2);
                        Logger.LogMessage("updateVersionStringOld:" + updateVersionStringOld);
                        Logger.LogMessage("updateVersionStringNew:" + updateVersionStringNew);

                        Version updateVersionNew = new Version(updateVersionStringNew);
                        if (string.IsNullOrWhiteSpace(updateVersionStringOld))
                            updateVersionStringOld = updateVersionNew.Major + ".0.0.0";
                        Version updateVersionOld = new Version(updateVersionStringOld);
                        

                        if (updateVersionOld.Major== updateVersionNew.Major && (updateVersionNew > updateVersionOld))
                        {
                            ApiHelper.DownloadFile("http://www.osoner.cn/upload/files/elite/updatefile.zip", zipfile2);
                            File.Copy(zipfile2, localUpdateFile, true);
                            File.Copy(versionfile2, localVersionFile, true);
                        }
                    }
                }
                catch (Exception ex) { Logger.LogError(ex); }

                try
                {
                    if (Environment.Is64BitOperatingSystem && !File.Exists("Help_x64.exe"))
                    {
                        ApiHelper.DownloadFile("http://www.osoner.cn/upload/files/Help_x64.exe", "Help_x64.exe");
                    }
                    if (!Environment.Is64BitOperatingSystem && !File.Exists("Help_x32.exe"))
                    {
                        ApiHelper.DownloadFile("http://www.osoner.cn/upload/files/Help_x32.exe", "Help_x32.exe");
                    }
                }
                catch (Exception ex) { Logger.LogError(ex); }
            };
            w.RunWorkerAsync();
        }

        void GetConver()
        {
            if (File.Exists("Cover.jpg"))
            {
                try
                {
                    using (FileStream fs = new FileStream("Cover.jpg", FileMode.Open, FileAccess.Read))
                    {
                        //通过调用系统的画笔工具，画出一个Image类型的数据，传给pictureBox。
                        Image im = System.Drawing.Bitmap.FromStream(fs);
                        pictureBox1.Image = im;
                        pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;

                        treeView1.BackColor = Color.AliceBlue;
                    }
                }
                catch (Exception ex)
                {
                    Logger.LogError(ex);
                }
            }
        }

        #region 左邊菜單
        string name = "_tree";
        string tsmort = "tsmOrt_";
        void InitialLeftMenu()
        {
            try
            {
                treeView1.Nodes.Clear();
                treeView1.ImageList = imageList1;
                //var ipqcmenus = BaseDataService.GetPqcProcess();
                var ipqcservice = new BaseDataTypeService(dbContext);
                var ipqcmenus = ipqcservice.GetUseDatas("ORT");
                foreach (BaseDataModel v in ipqcmenus)
                {
                    if (string.IsNullOrWhiteSpace(v.Name)) continue;

                    TreeNode node = new TreeNode(RemoveQuikKey(v.Name));
                    node.ImageIndex = 2;
                    node.SelectedImageIndex = 3;
                    node.Name = tsmort + v.Code;
                    node.Tag = v.Group + " " + v.Code;
                    //menu.Click += tsmReliability;
                    if (string.IsNullOrWhiteSpace(v.Group))
                    {
                        treeView1.Nodes.Add(node);
                    }
                    else
                    {
                        bool hasparent = false;
                        foreach (TreeNode menu2 in treeView1.Nodes)
                        {
                            if (menu2.Tag != null && menu2.Tag.ToString() == v.Group)
                            {
                                menu2.Nodes.Add(node);
                                hasparent = true;
                                break;
                            }
                        }

                        if (!hasparent)
                        {
                            TreeNode node2 = new TreeNode(RemoveQuikKey(v.Group));
                            node2.ImageIndex = 2;
                            node2.SelectedImageIndex = 3;
                            node2.Name = tsmort + v.Code;
                            node2.Tag = v.Group;
                            treeView1.Nodes.Add(node2);
                            node2.Nodes.Add(node);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.LogError(ex);
                //QessageBox.ShowException1(ex);
            }
        }
        void InitialLeftMenu2()
        {
            try
            {
                treeView1.Nodes.Clear();
                if(tsmBills.DropDownItems.Count>0)
                {
                    treeView1.ImageList = imageList1;
                    TreeNode node = new TreeNode(RemoveQuikKey("检测单"));
                    node.ImageIndex = 2;
                    node.SelectedImageIndex = 3;
                    node.Name = tsmOrt_TS.Name + name;
                    node.Tag = tsmOrt_TS;
                    treeView1.Nodes.Add(node);
                }
            }
            catch(Exception ex)
            {

            }
        }

        string RemoveQuikKey(string s)
        {
            string ss = s;

            try
            {
                if (s.IndexOf('(') > 0)
                {
                    ss = s.Substring(0, s.IndexOf('('));
                }
            }
            catch(Exception ex)
            {
                Logger.LogError(ex);
            }

            return ss;
        }
        void AddLeftMenu(TreeNode parent, ToolStripMenuItem item)
        {
            foreach (ToolStripMenuItem item2 in item.DropDownItems)
            {
                TreeNode node2 = new TreeNode(RemoveQuikKey(item2.Text));
                node2.Name = item2.Name + name;
                node2.ImageIndex = 0;
                node2.SelectedImageIndex = 1;
                parent.Nodes.Add(node2);
                if (item2.DropDownItems.Count > 0)
                {
                    AddLeftMenu(node2, item2);
                }
                else
                {
                    node2.ImageIndex = 2;
                    node2.SelectedImageIndex = 3;
                    node2.Tag = item2;
                }
            }
        }

        private void treeView1_DoubleClick(object sender, EventArgs e)
        {
            if (treeView1.SelectedNode != null && treeView1.SelectedNode.Tag != null)
            {
                tsmReliability_Click(treeView1.SelectedNode, new EventArgs());
            }
        }
#endregion 左邊菜單

        private void tsmUser_Click(object sender, EventArgs e)
        {
            try
            {
                FormUser f = new FormUser(this.user, dbContext);
                if (!UserRightService.CheckRight(f, this.user, RightType.Run))
                {
                    //QMessageBox.Show(noRightString, msgTitle);
                    f.Close();
                    return;
                }
                f.Show();
            }
            catch (Exception ex)
            {
                Logger.LogError(ex);
                QMessageBox.Show(ex.Message, "異常S0010", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #region 單據
        private void tsmFqc_Click(object sender, EventArgs e)
        {
            try
            {
                //FormFqc f = new FormFqc(user, this.dbContext);
                //if (!UserRightService.CheckRight(f, this.user, RightType.Run))
                //{
                //    f.Close();
                //    return;
                //}
                //f.Show();
            }
            catch (Exception ex)
            {
                Logger.LogError(ex);
                QMessageBox.Show(ex.Message, "異常S0085", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void tsmOqc_Click(object sender, EventArgs e)
        {
            try
            {
                //FormOqc f = new FormOqc(user, this.dbContext);
                //if (!UserRightService.CheckRight(f, this.user, RightType.Run))
                //{
                //    f.Close();
                //    return;
                //}
                //f.Show();
            }
            catch (Exception ex)
            {
                Logger.LogError(ex);
                QMessageBox.Show(ex.Message, "異常S0085", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #region 信賴性試驗
        void tsmReliability(string testType)
        {
            try
            {
                FormOrt f = new FormOrt(user, this.dbContext, testType);
                if (!UserRightService.CheckRight(f, this.user, RightType.Run))
                {
                    //QMessageBox.Show(noRightString, msgTitle);
                    f.Close();
                    return;
                }
                
                f.Show();
            }
            catch (Exception ex)
            {
                Logger.LogError(ex);
                QMessageBox.Show(ex.Message, "異常S0011", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void tsmReliability_Click(object sender, EventArgs e)
        {
            try
            {
                var menu = treeView1.SelectedNode;
                string billtype = string.Empty;
                if (menu != null) 
                {
                    if (menu.Name.Contains("_"))
                        billtype = menu.Name.Substring(menu.Name.LastIndexOf('_')+1);
                    else  billtype = menu.Name.Substring(menu.Name.Length - 2); 
                }

                if (!UserRightService.CheckRight("FormOrt", this.user, RightType.Run))
                {
                    return;
                }

                {
                    FormOrt f = new FormOrt(user, this.dbContext, billtype);
                    f.Text = f.Text + " " + menu.Text + " " + menu.Tag;
                    f.Show();
                }
            }
            catch (Exception ex)
            {
                Logger.LogError(ex);
                MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace
                    , "異常S0085", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void tsmOrt_TS_Click(object sender, EventArgs e)
        {
            //鋁電解電容高溫負荷壽命測試-樣品驗證
            tsmReliability("TS");
        }

        private void tsmOrt_LDT_Click(object sender, EventArgs e)
        {
            //鋁電解電容高溫負荷壽命測試 - 研究驗證1
            tsmReliability("LDT"); //LDT  LDW
        }

        private void tsmOrt_LDY_Click(object sender, EventArgs e)
        {
            //鋁電解電容高溫負荷壽命測試 - 研究驗證2
            tsmReliability("LDY");
        }

        private void tsmOrt_BL_Click(object sender, EventArgs e)
        {
            //鋁電解電容高溫負荷壽命測試 - 不良驗證
            tsmReliability("BL");
        }
        private void tsmOrt_A_Click(object sender, EventArgs e)
        {
            //鋁電解電容高溫負荷壽命測試 - 定期驗證
            tsmReliability("TC");
            
        }

        private void tsmOrt_SFTS_Click(object sender, EventArgs e)
        {
            //鋁電解電容高溫無負荷壽命測試 - 樣品驗證
            tsmReliability("SFTS");
        }

        private void tsmOrt_SFW_Click(object sender, EventArgs e)
        {
            //鋁電解電容高溫無負荷壽命測試 - 研究驗證1
            tsmReliability("SFLDT");//SFLDT   SFLDW
        }
        private void tsmOrt_SFLDY_Click(object sender, EventArgs e)
        {
            //鋁電解電容高溫無負荷壽命測試 - 研究驗證2
            tsmReliability("SFLDY");
        }


        private void tsmOrt_SFBL_Click(object sender, EventArgs e)
        {
            //鋁電解電容高溫無負荷壽命測試 - 不良驗證
            tsmReliability("SFBL");
        }

        private void tsmOrt_SF_Click(object sender, EventArgs e)
        {
            //鋁電解電容高溫無負荷壽命測試 - 定期驗證
            tsmReliability("SFTC");
        }

        private void tsmOrt_RC_Click(object sender, EventArgs e)
        {
            //高溫波紋壽命試驗 - 鋁電解電容
            tsmReliability("RC");
        }

        private void tsmOrt_PRC_Click(object sender, EventArgs e)
        {
            //高溫波紋壽命試驗 - 固態電容
            tsmReliability("PRC");
        }

        private void tsmOrt_LPD_Click(object sender, EventArgs e)
        {
            //固態電容高溫負荷壽命測試
            tsmReliability("LDP");
        }

        private void tsmOrt_LDR_Click(object sender, EventArgs e)
        {
            //固態電容高溫負荷壽命測試 - 研究驗證
            tsmReliability("LDR");
        }

        private void tsmOrt_SFP_Click(object sender, EventArgs e)
        {
            //固態電容高溫無負荷壽命測試
            tsmReliability("SFLDP");
        }

        private void tsmOrt_SFLDR_Click(object sender, EventArgs e)
        {
            //固態電容高溫無負荷壽命測試 - 研究驗證
            tsmReliability("SFLDR");
        }


        private void tsmOrt_YGR_Click(object sender, EventArgs e)
        {
            //SuCAP半液半固負荷壽命測試 - 研究驗證
            tsmReliability("YGR");
        }
        private void tsmOrt_YGL_Click(object sender, EventArgs e)
        {
            //SuCAP半液半固負荷壽命測試 - 量产
            tsmReliability("YGL");
        }

        private void tsmOrt_SFYGR_Click(object sender, EventArgs e)
        {
            //SuCAP半液半固無負荷壽命測試 - 研究驗證
            tsmReliability("SFYGR");
        }
        private void tsmOrt_SFYGL_Click(object sender, EventArgs e)
        {
            //SuCAP半液半固無負荷壽命測試 - 量产
            tsmReliability("SFYGL");
        }

        private void tsmOrt_S_Click(object sender, EventArgs e)
        {
            ////特殊试验
            //tsmReliability("S");
        }

        private void tsmOrt_S_S_Click(object sender, EventArgs e)
        {
            //特殊试验-量產
            tsmReliability("S");
        }

        private void tsmOrt_S_Y_Click(object sender, EventArgs e)
        {
            //特殊试验-研究驗證
            tsmReliability("Y");
        }
        #endregion 信賴性試驗

        private void tsmRd_Click(object sender, EventArgs e)
        {
            try
            {
                //FormRnd f = new FormRnd(user, this.dbContext);
                //if (!UserRightService.CheckRight(f, this.user, RightType.Run))
                //{
                //    //QMessageBox.Show(noRightString, msgTitle);
                //    f.Close();
                //    return;
                //}
                //f.Show();
            }
            catch (Exception ex)
            {
                Logger.LogError(ex);
                QMessageBox.Show(ex.Message, "異常S0011", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void tsmCompare_Click(object sender, EventArgs e)
        {
            try
            {
                //FormCom f = new FormCom(user, this.dbContext);
                //if (!UserRightService.CheckRight(f, this.user, RightType.Run))
                //{
                //    //QMessageBox.Show(noRightString, msgTitle);
                //    f.Close();
                //    return;
                //}
                //f.Show();
            }
            catch (Exception ex)
            {
                Logger.LogError(ex);
                QMessageBox.Show(ex.Message, "異常S0110", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        #endregion  單據

        #region 系統管理
        bool isClosing = false;
        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                if (!isClosing)
                {
                    if (QMessageBox.Show("您是否真的要退出檢測系統？", "請確認", MessageBoxButtons.OKCancel, MessageBoxIcon.Question)
                        == DialogResult.Cancel)
                    {
                        e.Cancel = true;
                        return;
                    }
                }
                isClosing = true;

                try
                {
                    LockService.DelAllLock(dbContext, user);
                    Settings.ClearTempFiles();
                }
                catch(Exception ex1)
                {

                }

                this.Hide();
                //Application.Exit();
                Environment.Exit(0);
            }
            catch(Exception ex)
            {
                Logger.LogError(ex);
            }
        }

        private void tsmUserGroup_Click(object sender, EventArgs e)
        {
            try
            {
                if (!UserRightService.CheckRight("FormUserGroup", this.user, RightType.Run))
                {
                    return;
                }
                FormUserGroup f = new FormUserGroup(this.user,this.dbContext);
                f.ShowDialog();
            }
            catch (Exception ex)
            {
                Logger.LogError(ex);
                QMessageBox.Show(ex.Message, "異常S0012", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void tsmCom_Click(object sender, EventArgs e)
        {
            try
            {
                if (!UserRightService.CheckRight("FormComSet", this.user, RightType.Run))
                {
                    return;
                }
                Machine.FormComSet f = new Machine.FormComSet(this.user);
                f.ShowDialog();
            }
            catch (Exception ex)
            {
                Logger.LogError(ex);
                QMessageBox.Show(ex.Message, "異常S0012", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void tsmReport_Click(object sender, EventArgs e)
        {
            try
            {
                FormReportSet f = new FormReportSet(user, dbContext);
                if (!UserRightService.CheckRight(f, this.user, RightType.Run))
                {
                    //QMessageBox.Show(noRightString, msgTitle);
                    f.Close();
                    return;
                }
                f.Show();
            }
            catch (Exception ex)
            {
                Logger.LogError(ex);
                QMessageBox.Show(ex.Message, "異常S0013", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void tsmSysParams_Click(object sender, EventArgs e)
        {
            try
            {
                FormSysParams f = new FormSysParams(this.user, dbContext);
                if (!UserRightService.CheckRight(f, this.user, RightType.Run))
                {
                    //QMessageBox.Show(noRightString, msgTitle);
                    f.Close();
                    return;
                }
                f.Show();
            }
            catch (Exception ex)
            {
                Logger.LogError(ex);
                QMessageBox.Show(ex.Message, "異常S0014", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void tsmRight_Click(object sender, EventArgs e)
        {
            try
            {
                FormUserRight f = new FormUserRight(this.user, dbContext);
                if (!UserRightService.CheckRight(f, this.user, RightType.Run))
                {
                    //QMessageBox.Show(noRightString, msgTitle);
                    f.Close();
                    return;
                }
                f.Show();
            }
            catch (Exception ex)
            {
                Logger.LogError(ex);
                QMessageBox.Show(ex.Message, "異常S0015", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void tsmLock_Click(object sender, EventArgs e)
        {
            try
            {
                FormLock f = new FormLock(this.user, dbContext);
                if (!UserRightService.CheckRight(f, this.user, RightType.Run))
                {
                    //QMessageBox.Show(noRightString, msgTitle);
                    f.Close();
                    return;
                }
                f.Show();
            }
            catch (Exception ex)
            {
                Logger.LogError(ex);
                QMessageBox.Show(ex.Message, "異常S0031", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void tsmLog_Click(object sender, EventArgs e)
        {
            try
            {
                FormLog f = new FormLog(this.user, dbContext);
                if (!UserRightService.CheckRight(f, this.user, RightType.Run))
                {
                    f.Close();
                    return;
                }
                f.Show();
            }
            catch (Exception ex)
            {
                Logger.LogError(ex);
                QMessageBox.Show(ex.Message, "異常S0032", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        private void tstExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }



        #endregion 系統管理

        #region  儀器設置
        private void tsmAutoTest1601_Click(object sender, EventArgs e)
        {
            try
            {
                if (!UserRightService.CheckRight("FormComSet", this.user, RightType.Run))
                {
                    return;
                }
                FormSet1601 f = new FormSet1601(this.user);
                f.Show();
            }
            catch (Exception ex)
            {
                Logger.LogError(ex);
                QMessageBox.Show(ex.Message, "異常S0072", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion 儀器設置

        private void tsmAbout_Click(object sender, EventArgs e)
        {
            FormAbout f = new FormAbout();
            f.Show();
        }

        private void 料號解析ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormProductCode f = new FormProductCode();
            f.Show();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {

        }

        private void tsmNail_Click(object sender, EventArgs e)
        {
            try
            {
            }
            catch (Exception ex)
            {
                Logger.LogError(ex);
                QMessageBox.Show(ex.Message, "異常S0085", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void 查询ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.user.UserCode != "oubl") return;
            FormSqlQuery f = new FormSqlQuery(dbContext);
            f.Show();
        }

        private void tsmToStatusBar_Click(object sender, EventArgs e)
        {
            notifyIcon1.Visible = true;
            this.ShowInTaskbar = false;
            this.Hide();
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            this.Show();                                //窗体显示
            this.WindowState = FormWindowState.Normal;  //窗体状态默认大小
            this.Activate();                            //激活窗体给予焦点
        }

        //隱藏
        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        //退出
        private void toolStripMenuItem3_Click(object sender, EventArgs e)
        {
            //点击"是(YES)"退出程序
            if (QMessageBox.Show("確定要退出程序?", "提示",
                        System.Windows.Forms.MessageBoxButtons.YesNo,
                        System.Windows.Forms.MessageBoxIcon.Warning)
                == System.Windows.Forms.DialogResult.Yes)
            {
                isClosing = true;
                notifyIcon1.Visible = false;   //设置图标不可见
                this.Close();                  //关闭窗体
                this.Dispose();                //释放资源
                Environment.Exit(0);            //关闭应用程序窗体
            }
        }

        private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
        {
            //点击鼠标"左键"发生
            if (e.Button == MouseButtons.Left)
            {
                this.Visible = true;                        //窗体可见
                this.WindowState = FormWindowState.Normal;  //窗体默认大小
                this.notifyIcon1.Visible = true;            //设置图标可见
            }
        }

        private void splitContainer1_SplitterMoved(object sender, SplitterEventArgs e)
        {

        }

        private void tsmHelpRemote_Click(object sender, EventArgs e)
        {
            try
            {
                if (Environment.Is64BitOperatingSystem && File.Exists("Help_64.exe"))
                    Process.Start("Help_64.exe");
                else if (!Environment.Is64BitOperatingSystem && File.Exists("Help_32.exe"))
                    Process.Start("Help_32.exe");
                else if (File.Exists("Help.exe"))
                    Process.Start("Help.exe");
            }
            catch (Exception ex)
            {

            }
        }

        private void tsmBaseDateToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if(user.IsAdmin)
            {
                FormBaseData f = new FormBaseData(user, dbContext, "ORT");
                f.Show();
            }
        }

        private void tsmHelp_Click(object sender, EventArgs e)
        {
            Process.Start("检测软件使用说明书.pdf");
        }

        private void tsmUpdateFileUpload_Click(object sender, EventArgs e)
        {
            try
            {
                string url = SysParamService.GetSysParamValue(dbContext, "UpdatePath");
                if (string.IsNullOrWhiteSpace(url))
                    url = AppDomain.CurrentDomain.BaseDirectory + "update";

                OpenFileDialog dialog = new OpenFileDialog();
                dialog.Multiselect = true;
                //dialog.Filter = "Excel|*.xls;*.xlsx";
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    Cursor = Cursors.WaitCursor;
                    if (dialog.FileNames.Length == 0) return;
                    StringBuilder sb = new StringBuilder();
                    bool flag = true;
                    foreach (string f in dialog.FileNames)
                    {
                        try
                        {
                            if (Path.GetExtension(f).EndsWith("exe")) continue;
                            File.Copy(f, Path.Combine(url, Path.GetFileName(f)), true);
                        }
                        catch (Exception ex)
                        {
                            flag = false;
                            QMessageBox.ShowException1(ex);
                            Logger.LogError(ex);
                        }
                    }

                    if (flag)
                        QMessageBox.ShowInformation("上传完成！");
                }
            }
            catch (Exception ex)
            {
                Logger.LogError(ex);
                QMessageBox.ShowException1(ex);
            }
            finally
            {
                Cursor = Cursors.Default;
            }
        }

        //操作日志
        private void tsmReportTemp_Click(object sender, EventArgs e)
        {
            try
            {
                string bmpurl = SysParamService.GetSysParamValue(dbContext, "ReportPath");
                if (!string.IsNullOrWhiteSpace(bmpurl))
                    Process.Start(bmpurl);
            }
            catch(Exception ex)
            {
                Logger.LogError(ex);
            }
        }

        //异常日志
        private void tsmExceptiionLog_Click(object sender, EventArgs e)
        {
            try
            {
                string Data = "ErrorLog";

                string s = Path.Combine(System.Environment.CurrentDirectory, Data);
                if (Directory.Exists(s))
                {
                    System.Diagnostics.Process.Start(s);
                }
            }
            catch { }
        }
    }
}
